<?php
require("../../utils/resolve_json.php");
require("../../utils/tools.php");
$result = array("code"=>3000,"msg"=>"登录失败,账号和密码不得为空!");
if(isset($data["id"])&&isset($data["pd"])){
    $exist_id = SQLTools::existTB($conn,"users","id='$data[id]'");
    if($exist_id){
        $re = SQLTools::queryTB($conn,"users","*","id","'$data[id]'"); // 查询结果在这个对象里面
        $user = $re->fetch_assoc();// 拉取一条数据
        if($user["pd"]){
            if(password_verify(md5($data["pd"]),$user["pd"])){// 验证密码
                /* 重新生成token */
                $token = create_token_sql($conn, 'users', 'token'); // 创建一个不重复的token
                if(SQLTools::updateTB($conn,"users","token='${token}',login_time=CURRENT_TIMESTAMP","token='${user['token']}'")){
                    $result["code"] = 2000;
                    $result["msg"] = "登录成功!";
                    $result["token"] = $token;
                }else{
                    $result["code"] = 3004;
                    $result["msg"] = "登录失败,生成验证信息失败!";
                }
            }else {
                $result["code"] = 3003;
                $result["msg"] = "登录失败,密码错误!";
            }
        }else{
            $result["code"] = 3002;
            $result["msg"] = "登录失败,密码验证错误!";
        }
    }else {
        $result["code"] = 3001;
        $result["msg"] = "登录失败,账号不存在!";
    }
}
echo json_encode($result);
?>